﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YAF.Classes;
using YAF.Classes.Data;
using YAF.Core;
using YAF.Types;
using YAF.Types.Constants;
using YAF.Types.Interfaces;
using YAF.Utils;

namespace YAF.controls
{
    public partial class HotNews : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            const string CacheKey = Constants.Cache.ForumActiveDiscussions;
            DataTable Mostview = null;
            if (Mostview == null)
            {
                this.Get<IYafSession>().UnreadTopics = 0;

                Mostview = LegacyDb.hoandd_topic_hotest(
                    boardID: this.PageContext.PageBoardID,
                    pageUserId: this.PageContext.PageUserID);

                if (this.PageContext.IsGuest)
                {
                    this.Get<IDataCache>().Set(
                        CacheKey,
                        Mostview,
                        TimeSpan.FromMinutes(this.Get<YafBoardSettings>().ActiveDiscussionsCacheTimeout));
                }
            }

            this.rptHotTopic.DataSource = Mostview;
            this.rptHotTopic.DataBind();
           
         /*   var newsID1 = ConfigurationManager.AppSettings["newsone"];
            if (newsID1 != null)
            {
                var news1 = LegacyDb.topic_info(newsID1);
                var topicSubject = this.Get<IBadWordReplace>().Replace(this.HtmlEncode(news1["TOPIC"]));
                HyperLinkNews1.Text = topicSubject;
                HyperLinkNews1.NavigateUrl = YafBuildLink.GetLinkNotEscaped(
                        ForumPages.posts, "t={0}", news1["TopicID"]);
            }
            var newsID2 = ConfigurationManager.AppSettings["newstwo"];
            if (newsID2 != null)
            {
                var news2 = LegacyDb.topic_info(newsID2);
                var topicSubject1 = this.Get<IBadWordReplace>().Replace(this.HtmlEncode(news2["TOPIC"]));
                HyperLinkNews2.Text = topicSubject1;
                HyperLinkNews2.NavigateUrl = YafBuildLink.GetLinkNotEscaped(
                        ForumPages.posts, "t={0}", news2["TopicID"]);
            }
            var newsID3 = ConfigurationManager.AppSettings["newsthree"];
            if (newsID3 != null)
            {
                var news3 = LegacyDb.topic_info(newsID3);
                var topicSubject2 = this.Get<IBadWordReplace>().Replace(this.HtmlEncode(news3["TOPIC"]));
                HyperLinkNews3.Text = topicSubject2;
                HyperLinkNews3.NavigateUrl = YafBuildLink.GetLinkNotEscaped(
                        ForumPages.posts, "t={0}", news3["TopicID"]);
            }
            var newsID4 = ConfigurationManager.AppSettings["newsfor"];
            if (newsID4 != null)
            {
                var news4 = LegacyDb.topic_info(newsID4);

                var topicSubject3 = this.Get<IBadWordReplace>().Replace(this.HtmlEncode(news4["TOPIC"]));
                HyperLinkNews4.Text = topicSubject3;
                HyperLinkNews4.NavigateUrl = YafBuildLink.GetLinkNotEscaped(
                        ForumPages.posts, "t={0}", news4["TopicID"]);
            }*/
        }


        protected void rptHotTopicItemDataBound([NotNull] object sender, [NotNull] RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
            {
                return;
            }
            var currentRow = (DataRowView)e.Item.DataItem;
            string messageUrl = YafBuildLink.GetLinkNotEscaped(
              ForumPages.posts, "m={0}&find=lastpost", currentRow["LastMessageID"]);
            var textMessageLink = (HyperLink)e.Item.FindControl("TextMessageLink");

            var styles = this.Get<YafBoardSettings>().UseStyledTopicTitles
                            ? this.Get<IStyleTransform>().DecodeStyleByString(currentRow["Styles"].ToString(), false)
                            : string.Empty;
            var topicSubject = this.Get<IBadWordReplace>().Replace(this.HtmlEncode(currentRow["TOPIC"]));
            if (styles.IsSet())
            {
                textMessageLink.Attributes.Add("style", styles);
            }
            if (currentRow["Status"].ToString().IsSet() && this.Get<YafBoardSettings>().EnableTopicStatus)
            {
                var topicStatusIcon = this.Get<ITheme>().GetItem("TOPIC_STATUS", currentRow["Status"].ToString());

                if (topicStatusIcon.IsSet() && !topicStatusIcon.Contains("[TOPIC_STATUS."))
                {
                    textMessageLink.Text =
                        "<img src=\"{0}\" alt=\"{1}\" title=\"{1}\" style=\"border: 0;width:16px;height:16px\" />&nbsp;{2}"
                            .FormatWith(
                                "../images/hot.gif",
                                this.GetText("TOPIC_STATUS", currentRow["Status"].ToString()),
                                topicSubject);
                }
                else
                {
                    textMessageLink.Text =
                        "[{0}]&nbsp;{1}".FormatWith(
                            this.GetText("TOPIC_STATUS", currentRow["Status"].ToString()), topicSubject);
                }
            }
            else
            {
                textMessageLink.Text = topicSubject;
            }

            if (!this.PageContext.IsMobileDevice)
            {
                textMessageLink.ToolTip = "{0}".FormatWith(
                    this.GetText("COMMON", "VIEW_TOPIC"));

                textMessageLink.NavigateUrl = YafBuildLink.GetLinkNotEscaped(
                    ForumPages.posts, "t={0}", currentRow["TopicID"]);
            }
            else
            {
                textMessageLink.ToolTip = this.GetText("DEFAULT", "GO_LASTUNREAD_POST");
                textMessageLink.NavigateUrl = YafBuildLink.GetLinkNotEscaped(
                    ForumPages.posts, "t={0}&find=unread", currentRow["TopicID"]);
            }

        }

    }
}